// pages/symptom/symptom.js
Page({
  data: {
    
  },
  onLoad(){

  },
  onShow() {

  },
  click:function(res){
    const query=wx.createSelectorQuery();
    query.select('#myCanvas').fields({ node: true, size: true }).exec((res)=>{
      const canvas = res[0].node
      const ctx = canvas.getContext('2d')
      const dpr = wx.getSystemInfoSync().pixelRatio
      canvas.width = res[0].width * dpr     // 获取宽
      canvas.height = res[0].height * dpr  // 获取高
      ctx.scale(dpr, dpr)
      let image = canvas.createImage();//创建iamge实例
      image.src = 'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1nSX9V.img?w=768&h=560&m=6&x=410&y=199&s=200&d=200';  // 引入本地图片//测试云存储
      image.onload = function () {
      ctx.drawImage(image, 0, 0, 80,80 ); // 背景图
     }
     setTimeout(function(){
      var temp=that.data.list;
      var lis=[];
      var data=ctx.getImageData(0,0,80,80).data;
      for (var i = 0; i < data.length; i += 4) {
       var r = data[i];
       var g = data[i + 1];
       var b = data[i + 2];
       var grey = r*0.3+g*0.59+b*0.11;
       var mid=[r,g,b];
       lis.push(mid);
       }
      temp.content=lis;
      let jsonString=JSON.stringify(temp);

      //const encoder=new TextEncoder("utf-8");
      //const bytes=encoder.encode(jsonString);
      const bytes=unescape(encodeURIComponent(jsonString)).split("").map(val => val.charCodeAt());
      var jstrig=decodeURIComponent(escape(String.fromCharCode(...bytes)));
      var test=JSON.parse(jstrig);
      var rgbcontent=test.content;

      //将rbgcontent转成可用的
      var imgData=ctx.getImageData(0,0,canvas.width,canvas.height);
      var pixelData=imgData.data;
      for(var index in rgbcontent){
        for(var j=0;j<rgbcontent[index].length;j++){
          pixelData[index*4+j] = rgbcontent[index][j];
        }
    }
    ctx.clearRect(0,0,canvas.width,canvas.height)
    ctx.putImageData(imgData , 200 , 0 ,0, 0 , canvas.width, canvas.height);
      },500)
    })
  }

})